package com.zany.net;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.springframework.context.ApplicationContext;


public class ServerHandler extends IoHandlerAdapter {
    public static Logger logger = Logger.getLogger(ServerHandler.class);

    private ApplicationContext context;

    public ServerHandler(ApplicationContext context) {
        this.context = context;
    }

    public void exceptionCaught(IoSession session, Throwable pArg1)
            throws Exception {
    }

    public void messageReceived(IoSession session, Object obj) throws Exception {
        if (logger.isInfoEnabled()) {
            logger.info("ServerHandler.messageReceived()");
        }
        // 收到的信息
        CommunicationHandler communicationHandler = new CommunicationHandler(obj.toString(), context);
        String result = communicationHandler.hendler();
        session.write(result);
    }

    public void messageSent(IoSession session, Object pArg1) throws Exception {
    }

    public void sessionClosed(IoSession session) throws Exception {
    }

    public void sessionCreated(IoSession session) throws Exception {
    }

    public void sessionIdle(IoSession session, IdleStatus pArg1)
            throws Exception {
    }

    public void sessionOpened(IoSession session) throws Exception {

    }

}
